<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class DesignDemandService extends BaseService
{
    //设计商城-需求

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($cateid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDesignDemand)->where('del', 0);
        if ($cateid) {
            $cateids = $this->getCateids($cateid);
            $db = $db->whereIn('cate_id', $cateids);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
            //分类信息
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $cateName = empty($cateOne) ? '-' : $cateOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'com_id' => $v['cid'],
                'com_name' => $cname,
                'com_logo' => $clogo,
                'title' => $v['title'],
                'cate_id' => $v['cate_id'],
                'cate_name' => $cateName,
                'area_id' => $v['area_id'],
                'detail' => htmlspecialchars_decode($v['detail']),
                'cr_detail' => $v['cr_detail'],
                'work_type' => $v['work_type'],
                'work_skill' => $v['work_skill'],
                'work_year' => $v['work_year'],
                'education' => $v['education'],
                'day_hour' => $v['day_hour'],
                'item_hour' => $v['item_hour'],
                'team_num' => $v['team_num'],
                'deadline' => $v['deadline'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    //一个
//    public function getOne($id)
//    {
//        $res = (array) $this->dbConn($this->dbDesignDemand)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        //企业信息
//        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
//        $cname = empty($comOne) ? '-' : $comOne['cname'];
//        //分类信息
//        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
//        $cateName = empty($cateOne) ? '-' : $cateOne['title'];
//        $arys = [
//            'title' => $res['title'],
//            'com_id' => $res['cid'],
//            'com_name' => $cname,
//            'cate_id' => $res['cate_id'],
//            'cate_name' => $cateName,
//            'area_id' => $res['area_id'],
//        ];
//        return $this->toback(0, '', $arys);
//    }

    //添加
    public function add($pas)
    {
//        $cateOne = (array) $this->dbConn($this->dbCate)
//            ->where('id', $pas['cate_id'])
//            ->first();
//        if (empty($cateOne)) {
//            return $this->toback(-2, '分类不存在');
//        }
        //数据入库
        $lastid = $this->dbConn($this->dbDesignDemand)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '发布失败');
        }
        return $this->toback();
    }
}